#include "MyDraw.h"

MyDraw::MyDraw()
{
	//控件初始化
	btn = new QPushButton("更改");
	colorBoxInit();
	styleBoxInit();
	drawWidgetInit();
	//水平布局
	QHBoxLayout *hLay = new QHBoxLayout();
	hLay->addWidget(colorBox);
	hLay->addWidget(styleBox);
	hLay->addWidget(btn);
	//垂直布局
	QVBoxLayout *vLay = new QVBoxLayout();
	vLay->addLayout(hLay);
	vLay->addWidget(drawWidget);
	//加载布局
	setLayout(vLay);
	//绑定信号和槽函数
	connect(btn, SIGNAL(clicked()), this, SLOT(changeSlot()));
	//重定义窗口大小
	resize(500,500);
//	drawWidget->setPalette(QPalette(Qt::white));
}
void MyDraw::colorBoxInit()
{
	colorBox = new QComboBox();
	QStringList color;
	color<<"红"<<"黄"<<"蓝"<<"绿"<<"黑";
	colorBox->addItems(color);
}
void MyDraw::styleBoxInit()
{
	styleBox = new QComboBox();
	QStringList style;
	style<<"实线"<<"虚线"<<"点划线";
	styleBox->addItems(style);
}
void MyDraw::drawWidgetInit()
{
	drawWidget = new QWidget;
	drawWidget->resize(400, 400);
}

void MyDraw::changeSlot()
{
	switch(colorBox->currentIndex())
	{
		case 0:
			pen.setColor(Qt::red);
			break;
		case 1:
			pen.setColor(Qt::yellow);
			break;
		case 2:
			pen.setColor(Qt::blue);
			break;
		case 3:
			pen.setColor(Qt::black);
			break;
		default:
			break;
	}
	switch(styleBox->currentIndex())
	{
		case 0:
			pen.setStyle(Qt::SolidLine);
			break;
		case 1:
			pen.setStyle(Qt::DashLine);
			break;
		case 2:
			pen.setStyle(Qt::DotLine);
			break;
		default:
			break;
	}
	//event
	update();
}

//绘图时间
void MyDraw::paintEvent(QPaintEvent *e)
{
	p.begin(this);
	p.setPen(pen);
	QBrush brush;
	brush.setStyle(Qt::CrossPattern);
	p.setBrush(brush);
	QRect rect(100, 100, 200, 400);
	p.drawEllipse(rect);
	p.end();
}
